package com.taotao.portal.controller;

import com.taotao.pojo.Item;
import com.taotao.pojo.form.OrderForm;
import com.taotao.portal.service.CartItemService;
import com.taotao.portal.service.OrderService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * describe:
 *
 * @author hong.zheng
 * @date 2018/09/25
 */
@RequestMapping("/order")
@Controller
public class OrderController {

    @Autowired
    private CartItemService cartItemService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("toMyOrder")
    public String toMyOrder(){
        return "my-orders";
    }

    @RequestMapping("/order-cart")
    public String showOrderCart(HttpServletRequest request, HttpServletResponse response, Model model) {
        //取购物车商品列表
        List<Item> list = cartItemService.getCartItemsList(request);
        //传递给页面
        model.addAttribute("cartList", list);
        return "order-cart";
    }
    @RequestMapping("/create")
    public String createOrder(OrderForm orderForm, Model model) {
        String orderId = orderService.createOrder(orderForm);
        model.addAttribute("orderId", orderId);
        model.addAttribute("payment", orderForm.getPayment());
        model.addAttribute("date", new DateTime().plusDays(3).toString("yyyy-MM-dd"));
        return "success";
    }


}
